﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Xml.Linq;

namespace Twi.COMMON.WPF
{
    public class RowXElement : XElement
    {
       

        public RowXElement(XElement xeRow, RowXElement rowParent, List<RowXElement> collection)
            : base(xeRow)
        {
            RowParent = rowParent;
            this.Level = 0;
            while (rowParent != null)
            {
                rowParent = rowParent.RowParent;
                this.Level++;
            }

            collection.Add(this);
            XElement xeItemList = xeRow.Elements().SingleOrDefault(x => 
                x.Name.LocalName == "ItemList" ||
               (x.Name.LocalName.EndsWith("List") && x.Name.LocalName == xeRow.Name.LocalName + "List"));
            if (xeItemList == null || !xeItemList.HasElements)
            {
                ExpanderVisibility = Visibility.Hidden;
            }
            else
            {
                RowChildren = new List<RowXElement>();
                ExpanderVisibility = Visibility.Visible;
                foreach (XElement subXe in xeItemList.Elements().ToArray())
                {
                    RowChildren.Add(new RowXElement(subXe, this, collection));
                }
            }
        }
 
        public RowXElement RowParent { get; set; }
        public List<RowXElement> RowChildren { get; set; }

        /// <summary>
        /// 将树形结构的RowXElement集合推平，返回新集合，但层级之间仍保持引用关系
        /// </summary>
        /// <returns></returns>
        public List<RowXElement> GetFlattenedChildren()
        {
            List<RowXElement> list = new List<RowXElement>();
            if (RowChildren != null)
            {
                foreach (RowXElement rx in RowChildren)
                {
                    list.Add(rx);
                    if (rx.RowChildren != null)
                    {
                        list.AddRange(rx.GetFlattenedChildren());
                    }
                }
            }
            return list;
        }

        public List<RowXElement> GetFlattenedChildrenAndSelf()
        {
            List<RowXElement> list = new List<RowXElement>();
            list.Add(this);
            if (RowChildren != null)
            {
                foreach (RowXElement rx in RowChildren)
                {
                    list.Add(rx);
                    if (rx.RowChildren != null)
                    {
                        list.AddRange(rx.GetFlattenedChildren());
                    }
                }
            }
            return list;
        }

        public bool IsExpanded { get; set; }

        public int Level
        {
            get;
            set;
        }

        public Visibility ExpanderVisibility { get; set; }

        /// <summary>
        /// 绑定行是否可视，控件的OnAsTreeViewChanged事件里设定了与该事件绑定的DataTrigger
        /// </summary>
        public bool IsVisible
        {
            get
            {
                if (this.Level == 0) //第一层
                {
                    return true;
                }
                else if (this.Level > 0)
                {
                    RowXElement rowParent = this.RowParent;
                    for (int i = 0; i < this.Level; i++)
                    {
                        if (!rowParent.IsExpanded) //如果总项未展开，那么子项也肯定不能显示
                        {
                            return false;
                        }
                        rowParent = rowParent.RowParent;
                    }
                    return true;
                }
                return false;
            }
        }


    }
}
